package com.yc.game.cms.service.impl;

import com.yc.game.cms.dao.CategoryMapper;
import com.yc.game.cms.pojo.Category;
import com.yc.game.cms.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 获取所有分类
     * @return
     */
    @Override
    public List<Category> getAll() {
        return categoryMapper.getAll();
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @Override
    public int updateCategory(Category category) {
        if (category.getId() == null) {
            Category newCategory = new Category();
            newCategory.setTitle(category.getTitle());
            newCategory.setStatus(category.getStatus());
            return categoryMapper.insertSelective(newCategory);
        }
        Category newCategory = new Category();
        newCategory.setId(category.getId());
        newCategory.setTitle(category.getTitle());
        newCategory.setStatus(category.getStatus());
        return categoryMapper.updateByPrimaryKeySelective(newCategory);
    }

    /**
     * 根据id删除信息
     * @param categoryId
     * @return
     */
    @Override
    public int deleteById(Integer categoryId) {
        return categoryMapper.deleteByPrimaryKey(categoryId);
    }
}
